//用来发送邮件
const nodemailer = require('nodemailer')

const { emailUser, emailpass } = require('../config/config_deafault')


let transporter = nodemailer.createTransport({
    service: 'qq',
    host: 'smtp.qq.com',
    prototype: 465,
    auth: {
        user: emailUser,
        pass: emailpass
    }
})


//生成随机验证码
function RandomNum(length) {
    const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'A', 'B', 'C', 'D', 'E', 'F']
    let result = ""
    for (let i = 0; i < length; i++) {
        result += arr[Math.floor(Math.random() * arr.length)]
    }
    return result
}

//发送邮件
function sendMail(mail, content) {
    return new Promise((resolve, reject) => {
        let mailOPtions = {
            from: "FromEMail <1723239430@qq.com>",
            to: mail,
            subject: "注册账号",
            html: content,
        }
        //发送邮件
        transporter.sendMail(mailOPtions, (error, info) => {
            if (error) {
                resolve({
                    code: 20001,
                    msg: error
                })
            }
            transporter.close()
            resolve({
                code: 0,
                msh: 'success'
            })
        })
    })

}

module.exports = {
    sendMail, RandomNum
}